#include "main_windw.h"
#include "ui_main_windw.h"
#include <QGuiApplication>
#include <QtQuickWidgets/QQuickWidget>
#include <QtNetwork>
#include "my_client.h"
#include "QMessageBox"

Main_windw::Main_windw(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::Main_windw)
{
    ui->setupUi(this);
    Init_Port();  // 初始化串口
    Init_param(); // 初始化参数结构体
    Load_sys_params();
    // 连接信号和槽，每当捕获到键盘事件时更新标签的内容
    if (init_param.run_mode == E_RUN_MODE_SERVER)
    {
        Open_server();
    }
    else if (init_param.run_mode == E_RUN_MODE_CLIENT)
    {
        Open_client();
    }

    connect(this, &Main_windw::Data_parse_signal, this, &Main_windw::Data_parse_slot);
}

void Main_windw::Close_server(void)
{
    if (my_server)
    {
        my_server->close();
        delete my_server;
        my_server = NULL;
        ui->groupBox_server->setTitle("服务器");
        ui->pushButton_connect_server->setEnabled(true);
        qDebug() << "Close_server ";
    }
}
void Main_windw::Open_server(void)
{
    if (!my_server)
    {
        my_server = new Server(this);
        connect(my_server, &Server::RecvDataSignal, this, &Main_windw::server_recv);
        connect(my_server, &Server::ConnectedSignal, this, &Main_windw::server_connect);
        connect(my_server, &Server::DisconnectedSignal, this, &Main_windw::server_disconnect);
        ui->groupBox_server->setTitle("本机服务器IP: " + my_server->HostAddr);
        ui->pushButton_connect_server->setEnabled(false);
        qDebug() << "Open_server ";
    }
}

void Main_windw::Close_client(void)
{
    if (client)
    {
        client->disconnectFromHost();
        client->close();
        delete client;
        client = NULL;
        qDebug() << "Close_client ";
    }
}
void Main_windw::Open_client(void)
{
    if (!client)
    {
        client = new Client(this);
        connect(client, &Client::RecvSignal, this, &Main_windw::client_recv);
        connect(client, &Client::ConnectedSignal, this, &Main_windw::client_connect);
        connect(client, &Client::DisconnectedSignal, this, &Main_windw::client_disconnect);
        qDebug() << "Open_client ";
    }
}

Main_windw::~Main_windw()
{
    set_sys_params(init_param);
    delete ui;
}
int getRandom(int min, int max)
{
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

    int num = qrand() % (max - min);
    qDebug() << num + min;
    return num;
}
/*
 * 弹窗显示,显示在软件窗口中心区域
 */
int Main_windw::show_my_tips(QString tital, QString text, int state)
{
    QPoint center = this->frameGeometry().center();
    int x = center.x() + 200; // 将横坐标增加100
    int y = center.y() + 200; // 将纵坐标增加100
    center = QPoint(x, y);
    QMessageBox msgBox;
    msgBox.setWindowTitle(tital);
    msgBox.setText(text);

    if (state)
    {
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    }
    else
    {
        msgBox.setStandardButtons(QMessageBox::Yes);
    }

    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.move(center - msgBox.rect().center());

    if (msgBox.exec() == QMessageBox::Yes)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
